package tothreedesignmodel.flyweight;

import java.util.HashMap;

/**
 * @Author lzhphantom
 * @Description
 * @Date 创建于 17:08 2020/7/16
 * @Modified
 */
public class FlyweightFactory {
    private static HashMap<String, Flyweight> pool = new HashMap<>();

    public static Flyweight getFlyweight(String key){
        Flyweight flyweight = pool.get(key);
        if (flyweight!=null){
            System.out.println(key+"is exist");
        }else {
            flyweight=new ConcreteFlyweight(key);
            pool.put(key,flyweight);
        }
        return flyweight;
    }

    public static void main(String[] args) {

        System.out.println(pool.size());
        Flyweight main = getFlyweight("main");
        System.out.println(pool.size());
        Flyweight english = getFlyweight("english");
        System.out.println(pool.size());
        Flyweight math = getFlyweight("math");
        System.out.println(pool.size());

    }
}
